

 

#include "cx_iostr.h"
#include "cx_memory.h"
 



int cx_iostr_Init( QIOStr * pStr, int iType, QBYTE * pucBuffer, int iBuflen )
{
	pStr->pucBuffer = pucBuffer;
	pStr->iBufLen = iBuflen;
	pStr->iType = iType;
	pStr->iIndex = 0;

	return QTRUE;
}
int cx_iostr_Reverse( QBYTE * pucBuffer, int iBufLen )
{
	int i;
	QBYTE ucTemp;
	int iLoopTime = iBufLen / 2;
	for( i = 0; i < iLoopTime; i++   )
	{
		ucTemp = pucBuffer[ i ];
		pucBuffer[ i ] = pucBuffer[ iBufLen - 1 - i ];
		pucBuffer[ iBufLen - 1 - i ] = ucTemp;
	}
	return QTRUE;
}

int cx_iostr_GetTotalLen( QIOStr * pStr )
{
	return pStr->iBufLen;
}
int cx_iostr_GetCurLen( QIOStr* pStr )
{
	return pStr->iIndex;

}


int cx_iostr_WriteQBYTE( QIOStr * pStr, QBYTE value )
{

	int iSize = sizeof( value );
	memcpy( pStr->pucBuffer + pStr->iIndex, &value, iSize );
	pStr->iIndex += iSize;

	return QTRUE;

}
int cx_iostr_WriteQSHORT( QIOStr * pStr, QSHORT value )
{
#ifdef NOT_HIGH_ORDER
	QSHORT nValue = value;
	
#else 
	QSHORT nValue = _HTONS_( value );
#endif

	int iSize = sizeof( value );
	memcpy( pStr->pucBuffer + pStr->iIndex, &nValue, iSize );
	pStr->iIndex += iSize;

	return QTRUE;
}
int cx_iostr_Writeint( QIOStr * pStr, int value, QBOOL bIsBigEndian )
{
	int nValue ;
	int iSize ;
	if ( bIsBigEndian ) {
		nValue = _HTONL_( value );
		
	}
	else {
	

#ifdef NOT_HIGH_ORDER
	 nValue = value;
	
#else 
	 nValue = _HTONL_( value );
#endif
	}

	iSize = sizeof( value );
	memcpy( pStr->pucBuffer + pStr->iIndex, &nValue, iSize );
	pStr->iIndex += iSize;

	return QTRUE;
}
int cx_iostr_Write( QIOStr * pStr, QBYTE * pucBuffer, int iBuflen )
{
	int iSize = iBuflen;
	memcpy( pStr->pucBuffer + pStr->iIndex, pucBuffer, iSize );
	pStr->iIndex += iSize;

	return QTRUE;
}

int cx_iostr_ReadQBYTE( QIOStr * pStr, QBYTE * value )
{
	int iSize = sizeof( *value );
	memcpy( value, pStr->pucBuffer + pStr->iIndex, iSize );
	pStr->iIndex += iSize;

	return QTRUE;
}
int cx_iostr_ReadQSHORT( QIOStr * pStr, QSHORT * value )
{
	QSHORT nValue;
	int iSize = sizeof( *value );
	memcpy( &nValue, pStr->pucBuffer + pStr->iIndex, iSize );
#ifdef NOT_HIGH_ORDER
	*value = nValue;
#else
	*value = _NTOHS_( nValue );
#endif

	pStr->iIndex += iSize;

	return QTRUE;
}
int cx_iostr_Readint( QIOStr * pStr, int * value, QBOOL bIsBigEndian )
{
	int nValue;
	int iSize = sizeof( *value );
	memcpy( &nValue, pStr->pucBuffer + pStr->iIndex, iSize );

	if ( bIsBigEndian ) {
		*value = _HTONL_( nValue );
	}
	else  {

#ifdef NOT_HIGH_ORDER
	*value = nValue;
#else
	*value = _HTONL_( nValue );
#endif
	}

	pStr->iIndex += iSize;

	return QTRUE;
}
int cx_iostr_Read( QIOStr * pStr, QBYTE * pucBuffer, int iBufLen )
{
	int iSize = iBufLen;
	memcpy( pucBuffer, pStr->pucBuffer + pStr->iIndex, iSize );
	pStr->iIndex += iSize;
	
	return QTRUE;
}
int cx_iostr_Skip( QIOStr * pStr, int iLength )
{
	pStr->iIndex += iLength;
	return QTRUE;
}


//////////////////////////////////////////////////////////////////////////////

static void cx_QIODynamicStr_KeepBufferEnough( QIODynamicStr * pStr, int nAddLen )
{
	//if ( pStr->stQIOStr.iIndex + nAddLen > pStr->stQIOStr.iBufLen ) {
	if ( pStr->iCurBufLen + nAddLen > pStr->stQIOStr.iBufLen ) {
		// not enough, enlarge it.
		QBYTE* pucLargeBuffer = cx_malloc( pStr->stQIOStr.iBufLen*2 + nAddLen );
		memcpy( pucLargeBuffer, pStr->stQIOStr.pucBuffer, pStr->stQIOStr.iIndex );
		pStr->stQIOStr.iBufLen = pStr->stQIOStr.iBufLen*2 + nAddLen;
		cx_free( pStr->stQIOStr.pucBuffer );
		pStr->stQIOStr.pucBuffer = pucLargeBuffer;

	}
	
	pStr->iCurBufLen += nAddLen;
}

QIODynamicStr*  cx_QIODynamicStr_Create(  int iReverseBuflen )
{
	QIODynamicStr* pMe = cx_malloc( sizeof( QIODynamicStr) );
   	pMe->stQIOStr.pucBuffer = cx_malloc( iReverseBuflen );
	pMe->stQIOStr.iBufLen = iReverseBuflen;
	pMe->iCurBufLen = 0;
	pMe->stQIOStr.iType = 0;
	pMe->stQIOStr.iIndex = 0;

	return pMe;
}
int cx_QIODynamicStr_Release( QIODynamicStr * pStr  )
{
 
	cx_free( pStr->stQIOStr.pucBuffer );
	pStr->stQIOStr.pucBuffer = NULL;

	cx_free( pStr );
	return QTRUE;
}

int cx_QIODynamicStr_GetTotalLen( QIODynamicStr * pStr )
{
	return pStr->stQIOStr.iBufLen;
}
int cx_QIODynamicStr_GetCurLen( QIODynamicStr* pStr )
{
	//return pStr->stQIOStr.iIndex;
	return pStr->iCurBufLen;

}

QBYTE* cx_QIODynamicStr_GetBuffer( QIODynamicStr * pStr,   int* piBuflen )
{
	//pucBuffer = pStr->stQIOStr.pucBuffer;
	*piBuflen = pStr->iCurBufLen;//pStr->stQIOStr.iIndex;
	return pStr->stQIOStr.pucBuffer;
	
}

int cx_QIODynamicStr_WriteQBYTE( QIODynamicStr * pStr, QBYTE value )
{

	cx_QIODynamicStr_KeepBufferEnough( pStr, sizeof( value ) );
	return cx_iostr_WriteQBYTE( &pStr->stQIOStr, value );

}

int cx_QIODynamicStr_WriteQSHORT( QIODynamicStr * pStr, QSHORT value )
{
	cx_QIODynamicStr_KeepBufferEnough( pStr, sizeof( value ) );
	return cx_iostr_WriteQSHORT( &pStr->stQIOStr, value );
}
int cx_QIODynamicStr_Writeint( QIODynamicStr * pStr, int value, QBOOL bIsBigEndian )
{
	cx_QIODynamicStr_KeepBufferEnough( pStr, sizeof( value ) );
	return cx_iostr_Writeint( &pStr->stQIOStr, value, bIsBigEndian );
}
int cx_QIODynamicStr_Write( QIODynamicStr * pStr, QBYTE * pucBuffer, int iBuflen )
{
	cx_QIODynamicStr_KeepBufferEnough( pStr, iBuflen );
	return cx_iostr_Write( &pStr->stQIOStr, pucBuffer, iBuflen );
}

void cx_QIODynamicStr_DeleteAllBuffer( QIODynamicStr * pStr )
{
	pStr->stQIOStr.iIndex = 0;
	pStr->iCurBufLen = 0;
}

int cx_QIODynamicStr_DeleteBuffer( QIODynamicStr * pStr, int iStartPos, int iLength )
{
	int ret = -1;
	if ( iStartPos < 0 || iLength <= 0 ) {
		return ret;
	}
	
	if ( iLength > pStr->stQIOStr.iIndex || ( iStartPos + iLength ) > pStr->stQIOStr.iIndex ) { // excel the max len
		return ret;
	}
	
	pStr->stQIOStr.iIndex = 0;
	
	if ( iStartPos == 0 && iLength == pStr->iCurBufLen   ) {
		// clear all
		pStr->iCurBufLen = 0;
		
	}
	else {
		cx_memmove( pStr->stQIOStr.pucBuffer + iStartPos, pStr->stQIOStr.pucBuffer + iStartPos + iLength, pStr->iCurBufLen -iStartPos - iLength ) ;
		pStr->iCurBufLen -= iLength;
		pStr->iCurBufLen = ( pStr->iCurBufLen <= 0 ) ? 0 : pStr->iCurBufLen;
	}
	ret = iLength;
	return ret;
		
}

int cx_QIODynamicStr_ReadQBYTE( QIODynamicStr * pStr, QBYTE * value )
{
	 

	return cx_iostr_ReadQBYTE( &pStr->stQIOStr, value );
}
int cx_QIODynamicStr_ReadQSHORT( QIODynamicStr * pStr, QSHORT * value )
{
	return cx_iostr_ReadQSHORT( &pStr->stQIOStr, value );
	 
}
int cx_QIODynamicStr_Readint( QIODynamicStr * pStr, int * value, QBOOL bIsBigEndian )
{
	return cx_iostr_Readint( &pStr->stQIOStr, value, bIsBigEndian );
	 
}
int cx_QIODynamicStr_Read( QIODynamicStr * pStr, QBYTE * pucBuffer, int iBufLen )
{
	return cx_iostr_Read( &pStr->stQIOStr, pucBuffer,iBufLen );

	 
}
int cx_QIODynamicStr_Skip( QIODynamicStr * pStr, int iLength )
{
	return cx_iostr_Skip( &pStr->stQIOStr, iLength );
	 
}